package music;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

import java.util.HashMap;
import java.util.Map;

@Aspect
public class Counter {

    private Map<String, Integer> counts = new HashMap<String, Integer>();


    @Pointcut("execution(* music.Player.play(String)) && args(songName)")
    public void trackPlay(String songName){}

    // 播放歌曲之前记录该歌曲的播放次数
    @Before("trackPlay(songName)")
    public void countTrack(String songName){
        Integer currentCount = counts.getOrDefault(songName, Integer.valueOf("0"));
        counts.put(songName, currentCount + 1);
    }

    public int getPlayCount(String songName){
        return counts.containsKey(songName)? counts.get(songName) : 0;
    }
}
